package servlet;

import com.alibaba.fastjson.JSON;
import entity.Goods;
import model.Picture;
import urity.Page;
import service.GoodsService;
import service.impl.GoodsServiceImpl;
import vo.FlowVo;
import vo.SalesVolumeVo;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@WebServlet("/GoodsServlet")
@MultipartConfig
public class GoodsServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        //定义session对象
        HttpSession session = request.getSession();
        //创建Writer对象
        PrintWriter out = response.getWriter();
        //判断opr值
        //获取参数
        String opr = request.getParameter("opr");
        if (opr.equals("pagelist")) {
            String pageNo = request.getParameter("pageNo");
            if (pageNo == null || pageNo.equals("")) {
                pageNo = "1";
            }
            int pageSize = 7;
            //获取查询框的值
            String textbox = request.getParameter("textbox");
            request.setAttribute("textbox", textbox);
            String sort = request.getParameter("sort");

            //下拉框闯入ses对象
            request.setAttribute("sort", sort);

            GoodsService service = new GoodsServiceImpl();
            Page page = service.selectPage(textbox, sort, Integer.parseInt(pageNo), pageSize, opr);
            //放入数据，并且转发向到网页
            request.setAttribute("page", page);
            request.getRequestDispatcher("product_list.jsp").forward(request, response);
        } else if (opr.equals("recycle")) {
            String pageNo = request.getParameter("pageNo");
            if (pageNo == null || pageNo.equals("")) {
                pageNo = "1";
            }
            int pageSize = 7;
            //获取查询框的值
            String textbox = request.getParameter("textbox");
            request.setAttribute("textbox", textbox);

            String sort = request.getParameter("sort");

            //下拉框闯入ses对象
            request.setAttribute("sort", sort);

            GoodsService service = new GoodsServiceImpl();

            Page page = service.selectPage(textbox, sort, Integer.parseInt(pageNo), pageSize, opr);

            request.setAttribute("page", page);
            request.getRequestDispatcher("recycle_bin.jsp").forward(request, response);

            //上/下架商品
        } else if (opr.equals("delect")) {
            //获取参数
            String state = request.getParameter("state");
            if (state.equals("1")) {
                state = "0";
            } else if (state.equals("0")) {
                state = "1";
            }

            String id = request.getParameter("id");
            //获取当前时间并转成字符串
            DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            LocalDateTime ldt = LocalDateTime.now();
            String time = dtf.format(ldt);
            //创建对象实现方法
            GoodsService service = new GoodsServiceImpl();
            int delect = service.delect(Integer.parseInt(state), time, Integer.parseInt(id));
            //分析返回结果

            if (delect > 0) {
                out.print(true);
            } else {
                out.print(false);
            }
        } else if (opr.equals("detail")) {
            //查看详情
            GoodsService service = new GoodsServiceImpl();
            String id = request.getParameter("id");
            Goods goods = service.selectOne(Integer.parseInt(id));
            request.setAttribute("goods", goods);

            request.getRequestDispatcher("product_insert.jsp").forward(request, response);
        } else if (opr.equals("delect2")) {
            GoodsService service = new GoodsServiceImpl();
            String id = request.getParameter("id");
            int rs = service.goodsdelect(Integer.parseInt(id));
            if (rs > 0) {
                out.print(true);
            } else {
                out.print(false);
            }
        } else if (opr.equals("update")) {
            //商品名称
            String name = request.getParameter("goodsName");
            Goods goods = new Goods();
            goods.setGoodsName(name);
            //商品价格
            double price = Double.parseDouble(request.getParameter("price"));
            goods.setPrice(price);
            //商品id
            int id = Integer.parseInt(request.getParameter("id"));
            goods.setGoodsID(id);
            //商品描述
            String describe = request.getParameter("describe");
            goods.setDescribe(describe);

            //获取当前时间并转成字符串
            DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            LocalDateTime ldt = LocalDateTime.now();
            String time = dtf.format(ldt);

            //商品上架时间
            goods.setGoodsTime(time);
            //商品库存
            int inventory = Integer.parseInt(request.getParameter("inventory"));
            goods.setInventory(inventory);
            //单位
            String units = request.getParameter("units");
            goods.setUnits(units);
            //商品图片路径
            String url = "Picture/";
            //图片绝对路径
            String realPath = getServletContext().getRealPath(url);
            //获取图片
            Part filePath = request.getPart("filePath");

            //获取 图片路径+名称
            Picture picture = new Picture();
            String s = picture.filesUploadModel(filePath, realPath);

            String location = url + s;
            goods.setFilePath(location);
            goods.setPicturename(s);
            //获取商品分类信息
            int classify = Integer.parseInt(request.getParameter("classify"));
            goods.setClassifys(classify);

            GoodsService service = new GoodsServiceImpl();

            service.update(goods);

            out.print("<script>location.href='GoodsServlet?opr=pagelist&pageNo=1';</script>");
        } else if (opr.equals("list")) {
            GoodsService service = new GoodsServiceImpl();
            List<SalesVolumeVo> lists = service.selectSum();

            String list = JSON.toJSONString(lists);


            out.print(list);


        } else if (opr.equals("list2")) {
            GoodsService service = new GoodsServiceImpl();
            List<FlowVo> lists = service.selectFlow();

//            String list2 = JSON.toJSONString(lists);

//
//            out.print(list2);

            String str="[";
            for (FlowVo list : lists) {
                if(!str.equals("[")) {
                    str+=",";
                }
                str+="{'name': '"+list.getMonths()+"', 'value': '"+list.getSalesVolume()+"'}";
            }
            str+="]";

            request.setAttribute("list",str);
//            request.setAttribute("list2",lists);
            request.getRequestDispatcher("discharge_statistic.jsp").forward(request,response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
